package controllers

import (
	"github.com/gin-gonic/gin"
	"github.com/tommy351/gin-sessions"
	"strconv"
	"strings"
	"time"
	"wallet/models"
)

func SelectBillList(c *gin.Context) {
	bookID := c.Query("bookId")
	bid, err := strconv.Atoi(bookID)
	if err != nil {
		c.JSON(200, gin.H{
			"message": err.Error(),
		})
	} else {
		//c.JSON(200, gin.H{
		//	"data": models.SelectBillListByBookID(uint(bid)),
		//})
		c.HTML(200, "bill.html", gin.H{
			"data":   models.SelectBillListByBookID(uint(bid)),
			"users":  models.SelectUserListByBookID(uint(bid)),
			"bookId": bookID,
		})
	}
}

func DeleteBill(c *gin.Context) {
	billID := c.Query("id")
	_billid, _ := strconv.Atoi(billID)
	if models.DeleteBill(uint(_billid)) {
		c.JSON(200, gin.H{
			"succeed": true,
			"message": "succeed",
		})
	}
}

func AddBill(c *gin.Context) {

	s := sessions.Get(c)
	user := s.Get("current_user")
	value, ok := user.(*models.User)
	if !ok {
		return
	}

	title := c.PostForm("title")
	cate := c.PostForm("cate")
	account := c.PostForm("account")
	occurdate := c.PostForm("occurdate")
	casherId := c.PostForm("casherId")
	total := c.PostForm("total")
	invitorId := c.PostForm("invitorId")
	bookId := c.PostForm("bookId")
	detail := c.PostForm("detail")
	users := c.PostForm("users")
	remark := c.PostForm("remark")

	if len(title) == 0 || len(account) == 0 || len(occurdate) == 0 || len(casherId) == 0 || len(total) == 0 || len(bookId) == 0 || len(users) == 0 {
		c.JSON(200, gin.H{
			"succeed": false,
			"message": "missing parameter",
		})
		return
	}

	if len(cate) == 0 || !(cate == "+" || cate == "-") {
		cate = "-"
	}
	_occurdate, _ := time.Parse("2006-01-02", occurdate)
	_value, _ := strconv.ParseFloat(account, 32)
	_invitorId, _ := strconv.Atoi(invitorId)
	_casherId, _ := strconv.Atoi(casherId)
	_bookId, _ := strconv.Atoi(bookId)
	reallycount := models.CountUserByBookID(uint(_bookId))
	_total, _ := strconv.Atoi(total)
	if _total < reallycount {
		if _total != len(strings.Split(users, ",")) {
			c.JSON(200, gin.H{
				"succeed": false,
				"message": "total doesn't match users.",
			})
			return
		}
	}
	participants := "$" + strings.Join(strings.Split(users, ","), "#$") + "#"
	//fmt.Println("participants:", participants)

	userbooks := models.SelectUserBookByBookID(uint(_bookId))
	// 检查邀请人是否正确
	var flag bool
	if len(invitorId) > 0 {
		for _, ub := range userbooks {
			if ub.UserID == uint(_invitorId) {
				flag = true
				break
			}
		}
		if !flag {
			c.JSON(200, gin.H{
				"succeed": false,
				"message": "error invitorId.",
			})
			return
		}
	}
	// 检查付款人是否正确
	flag = false
	for _, ub := range userbooks {
		if ub.UserID == uint(_casherId) {
			flag = true
			break
		}
	}
	if !flag {
		c.JSON(200, gin.H{
			"succeed": false,
			"message": "error casherId.",
		})
		return
	}

	// 检查参与消费人是否正确
	flag = true

	for _, userid := range strings.Split(users, ",") {
		var find bool
		_userid, _ := strconv.Atoi(userid)
		for _, ub := range userbooks {
			if uint(_userid) == ub.UserID {
				find = true
				//break
			}
		}
		if !find {
			flag = false
			break
		}
	}
	if !flag {
		c.JSON(200, gin.H{
			"succeed": false,
			"message": "error invitorId.",
		})
		return
	}

	if models.InsertBill(title, cate, detail, participants, remark, float32(_value), _occurdate, uint(_casherId), uint(_invitorId), uint(_bookId), value.ID, int8(_total)) {
		c.JSON(200, gin.H{
			"succeed": true,
			"message": "succeed",
		})
	} else {
		c.JSON(200, gin.H{
			"succeed": false,
			"message": "failed",
		})
	}

}
